package dca.sistemascontrole.view;

import dca.sistemascontrole.domain.DataObject;
import dca.sistemascontrole.domain.VariaveisAnalise;
import java.util.List;

/**
 *
 * @author Rafael
 */
public class GeradorAnalises extends javax.swing.JDialog {

    private VariaveisAnalise variaveisAnalise;

    public GeradorAnalises(java.awt.Frame parent, boolean modal, VariaveisAnalise variaveisAnalise) {
        super(parent, modal);
        initComponents();

        this.variaveisAnalise = variaveisAnalise;
    }

    public void gerarAnalises() {
        //TODO Completar
        double mp;
        boolean determinouTS = false;
        boolean determinouTP = false;
        double setpoint = variaveisAnalise.getSetpoint();
        List<DataObject> variavelAnalisada = variaveisAnalise.getNivelTanqueSuperior();

        double va = variavelAnalisada.get(0).getValue();
        double v1 = variavelAnalisada.get(1).getValue();
        double vf = variavelAnalisada.get(variavelAnalisada.size() - 1).getValue();
        long t0 = getTimeIn_ms(variavelAnalisada.get(0));
        long ta = t0;
        long t1 = getTimeIn_ms(variavelAnalisada.get(1));
        double derivada;
        double derivadaAnt = (v1 - va) / (t1 - ta);

        ta = t1;
        va = v1;
        for (int i = 2; i < variavelAnalisada.size(); i++) {
            DataObject dataObject = variavelAnalisada.get(i);
            v1 = dataObject.getValue();
            t1 = getTimeIn_ms(dataObject);

            if (v1 > setpoint && !determinouTS) {
                determinouTS = true;
                txtTempoSubida.setText(String.valueOf(t1 - t0) + "ms");
            }

            derivada = (v1 - va) / (t1 - ta);
            if (derivadaAnt >= 0 && derivada < 0 && v1 > setpoint && !determinouTP) {
                determinouTP = true;
                txtTempoPico.setText(String.valueOf(t1 - t0) + "ms");

                mp = (v1 - vf) / vf * 100;
                txtSobreSinal.setText(String.valueOf(mp) + "%");
            }
            
            if (determinouTP && determinouTS) {
                break;
            }

            ta = t1;
            va = v1;
            derivadaAnt = derivada;
        }

        calculaAcomodacao(variavelAnalisada, setpoint);
    }

    public void mostrarAnalise() {
        setVisible(true);
    }

    private long getTimeIn_ms(DataObject dataObject) {
        return dataObject.getMillisecond().getTime().getTime();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTempoSubida = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtSobreSinal = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTempoPico = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtTempoAcomodacao = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Tempo Subida:");

        jLabel2.setText("Sobre-sinal %:");

        txtSobreSinal.setText("Sem sobre-sinal");

        jLabel3.setText("Tempo de pico:");

        jLabel4.setText("Tempo Acomdação (2%):");

        txtTempoAcomodacao.setText("Não ocorreu acomodação");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoSubida))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoAcomodacao, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSobreSinal))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTempoPico)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTempoSubida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSobreSinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTempoPico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtTempoAcomodacao)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, txtSobreSinal, txtTempoPico, txtTempoSubida});

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtSobreSinal;
    private javax.swing.JTextField txtTempoAcomodacao;
    private javax.swing.JTextField txtTempoPico;
    private javax.swing.JTextField txtTempoSubida;
    // End of variables declaration//GEN-END:variables

    private void calculaAcomodacao(List<DataObject> dataObjects, double setpoint) {
        boolean acomodacaoFinal, acomodacao;
        int size = dataObjects.size();
        double v, t, tf, t0;

        t0 = getTimeIn_ms(dataObjects.get(0));
        tf = getTimeIn_ms(dataObjects.get(size - 1));
        v = dataObjects.get(size - 1).getValue();
        acomodacaoFinal = (v / setpoint) >= 0.98 && (v / setpoint) <= 1.02;

        if (acomodacaoFinal) {
            for (int i = 2; i < size; i++) {
                t = getTimeIn_ms(dataObjects.get(size - i));
                v = dataObjects.get(size - i).getValue();
                acomodacao = (v / setpoint) >= 0.98 && (v / setpoint) <= 1.02;

                if (!acomodacao) {
                    txtTempoAcomodacao.setText(String.valueOf(t - t0) + "ms");
                    return;
                }
            }
        }
    }
}
